//
//  SpeakViewController.m
//  baodongPro
//
//  Created by feng on 16/3/1.
//  Copyright © 2016年 proper. All rights reserved.
//
#import "iflyMSC/IFlyRecognizerViewDelegate.h"
#import "iflyMSC/IFlyRecognizerView.h"
#import "iflyMSC/IFlySpeechConstant.h"
#import "iflyMSC/IFlySpeechUtility.h"
#import "SpeakViewController.h"

@interface SpeakViewController ()<IFlyRecognizerViewDelegate>
{
    IFlyRecognizerView* _recordSpeakView;
    UILabel*testLabelView;
    NSString *result;
}
@end

@implementation SpeakViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.containedView setView_addSizeHeightBy:-80];
    // Do any additional setup after loading the view.
    [self.containedView  setBackgroundColor:[UIColor brownColor]];
    testLabelView=[self.containedView fbd_quickCreateUILabelWithFrame:CGRectMake(0, 10, ScreenW, 100)];
    testLabelView.numberOfLines=0;
    
    [self.containedView addSubview:testLabelView];
    
    result =@"开始：";
    UIView*testView2=[self.containedView fbd_quickCreateUIViewWithFrame:CGRectMake(0, CGRectGetHeight(self.containedView.frame)-64, ScreenW, 100)];
    [self.containedView addSubview:testView2];
    
    
    //创建语音配置,appid必须要传入，仅执行一次则可
    NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"56d67e40"];
    
    
    //所有服务启动前，需要确保执行createUtility
    [IFlySpeechUtility createUtility:initString];
    
    _recordSpeakView=[[IFlyRecognizerView alloc] initWithCenter:CGPointMake(screenW/2.0, 200)];
    _recordSpeakView.delegate=self;
    
    [self.containedView addSubview:_recordSpeakView];
    
    
    
    
    BOOL first=[_recordSpeakView setParameter: @"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];
    
    //设置结果数据格式，可设置为json，xml，plain，默认为json。
    BOOL second=    [_recordSpeakView setParameter:@"plain" forKey:[IFlySpeechConstant RESULT_TYPE]];
    BOOL third= [_recordSpeakView setParameter:@"asrview.pcm " forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];
    NSLog(@"%d%d%d",first,second,third);
    [_recordSpeakView start];
    
//    [self performSelector:@selector(cancelTextRecord) withObject:nil afterDelay:2];
    
    
}

-(void)cancelTextRecord
{

//    [_recordSpeakView cancel];

}





#pragma  mark ------- IFlyRecognizerViewDelegate


/** 回调返回识别结果
 
 @param resultArray 识别结果，NSArray的第一个元素为NSDictionary，NSDictionary的key为识别结果，value为置信度
 @param isLast      -[out] 是否最后一个结果
 */
- (void)onResult:(NSArray *)resultArray isLast:(BOOL) isLast
{

//    NSLog(@"resultArray>>>>>>%@\n isLast >>>>>>%d",resultArray,isLast);
    
    
    
    
    NSDictionary *dic = [resultArray objectAtIndex:0];
    
    for (NSString *key in dic)
    {
        result=[NSString  stringWithFormat:@"%@%@",result,key];
        testLabelView.text=result;
        
    }
 
    if (isLast)
    {
        result=@"";
    }


}

/** 识别结束回调
 
 @param error 识别结束错误码
 */
- (void)onError: (IFlySpeechError *) error
{






}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
